// 封装方法，给localStorage设置有效期
export default class localStorageEx{
    constructor() {
        this.name = 'localStorageEx'
    }
    // 设置存储
    // key:需要存储的数据key
    // value:需要存储的数据value
    setItem(key,value) {
        let storage = localStorage.getItem(window.gconfig.localStorageNamespace);
        storage = storage ? JSON.parse(storage) : {};
        storage[key] = value;
        localStorage.setItem(window.gconfig.localStorageNamespace, JSON.stringify(storage));
    }

    // 获取存储数据
    getItem(key) {
        let storage = localStorage.getItem(window.gconfig.localStorageNamespace)
        if (!storage) return undefined;
        storage = JSON.parse(storage);
        const flag = storage[key];
        return flag || undefined;
    }

    //移出缓存
    removeItem(name){
        let storage = localStorage.getItem(window.gconfig.localStorageNamespace)
        console.log(storage)
        storage = storage ? JSON.parse(storage) : {};
        let nameStorage = storage[name];
        if(!nameStorage) return false
        delete storage[name];
        localStorage.setItem(window.gconfig.localStorageNamespace,JSON.stringify(storage));
    }

    //移出当前缓存
    clear(){
        localStorage.removeItem(window.gconfig.localStorageNamespace);
    }

    //移出所有缓存
    clearAll(){
        localStorage.clear();
    }
}
